﻿
using UnityEngine;

namespace Nirvana
{
    public sealed class UILayerData
    {
        public string m_key;
        /// <summary>
        /// 当前UI架构渲染的Camera
        /// </summary>
        public Camera m_camera;
        /// <summary>
        /// 当前UI架构的父节点
        /// </summary>
        public GameObject m_root;
        /// <summary>
        /// 每层UI的挂载节点
        /// </summary>
        public Transform m_FixedLayerParent;
        public Transform m_NormalLayerParent;
        public Transform m_PopUpLayerParent1;
        public Transform m_PopUpLayerParent2;
        public Transform m_PopUpLayerParent3;
        public Transform m_TopLayerParent;
    }

    /// <summary>
    /// UI图层管理器
    /// </summary>
    public sealed class UILayerManager : MonoBehaviour
    {

        public UILayerData UILayerList;

        // Start is called before the first frame update
        void Awake()
        {
            InitLayerNode();
            CheckLayerNode();
        }
        private void InitLayerNode()
        {
            var mTrans = transform;
            UILayerList = new UILayerData();
            UILayerList.m_root = gameObject;
            UILayerList.m_camera = mTrans.Find("UICamera").GetComponent<Camera>();
            UILayerList.m_FixedLayerParent = mTrans.Find("DefaultUI/Fixed");
            UILayerList.m_NormalLayerParent = mTrans.Find("DefaultUI/Normal");
            UILayerList.m_PopUpLayerParent1 = mTrans.Find("DefaultUI/PopUp1");
            UILayerList.m_PopUpLayerParent2 = mTrans.Find("DefaultUI/PopUp2");
            UILayerList.m_PopUpLayerParent3 = mTrans.Find("DefaultUI/PopUp3");
            UILayerList.m_TopLayerParent = mTrans.Find("DefaultUI/Top");
       
        }

        //检测每个UI架构的完整性
        private void CheckLayerNode()
        {
            var data = UILayerList;
            //每个UI架构偏移深度
            data.m_root.transform.localPosition = new Vector3(0, 0, 0);

            if (data.m_root == null)
            {
                Debug.LogError("UILayerManager :Root is null! " + " key : " + data.m_key );
            }

            if (data.m_camera == null)
            {
                Debug.LogError("UILayerManager :Camera is null! " + " key : " + data.m_key );
            }

            if (data.m_FixedLayerParent == null)
            {
                Debug.LogError("UILayerManager :FixedLayerParent is null!" + " key : " + data.m_key );
            }

            if (data.m_NormalLayerParent == null)
            {
                Debug.LogError("UILayerManager :NormalLayerParent is null!" + " key : " + data.m_key );
            }

            if (data.m_TopLayerParent == null)
            {
                Debug.LogError("UILayerManager :TopLayerParent is null!" + " key : " + data.m_key );
            }

            if (data.m_PopUpLayerParent1 == null)
            {
                Debug.LogError("UILayerManager :popUpLayerParent1 is null!" + " key : " + data.m_key );
            }
            if (data.m_PopUpLayerParent2 == null)
            {
                Debug.LogError("UILayerManager :popUpLayerParent2 is null!" + " key : " + data.m_key );
            }
            if (data.m_PopUpLayerParent3 == null)
            {
                Debug.LogError("UILayerManager :popUpLayerParent3 is null!" + " key : " + data.m_key );
            }
        }
        public void SetLayer(UIWindowBase ui,string layerKey = null)
        {
            var data = GetUILayerDataByKey(string.IsNullOrEmpty(layerKey) ? ui.layerKey :layerKey);
            var rt = ui.GetComponent<RectTransform>();

            switch (ui.m_UIType)
            {
                case UIType.Fixed: ui.transform.SetParent(data.m_FixedLayerParent); break;
                case UIType.Normal: ui.transform.SetParent(data.m_NormalLayerParent); break;
                case UIType.PopUp1: ui.transform.SetParent(data.m_PopUpLayerParent1); break;
                case UIType.PopUp2: ui.transform.SetParent(data.m_PopUpLayerParent2); break;
                case UIType.PopUp3: ui.transform.SetParent(data.m_PopUpLayerParent3); break;
                case UIType.Top: ui.transform.SetParent(data.m_TopLayerParent); break;
            }
            rt.transform.localPosition = Vector3.zero;
            rt.localScale = Vector3.one;
            rt.sizeDelta = Vector2.zero;
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.one;
            rt.anchoredPosition = Vector3.zero;
            rt.SetAsLastSibling();

            var canvas = ui.GetOrAddComponent<Canvas>();
            canvas.overrideSorting = true;
            canvas.sortingLayerID = SortingLayer.NameToID(ui.m_UIType.ToString());
            
            ui.GetOrAddComponent<UnityEngine.UI.GraphicRaycaster>();
        }

        /// <summary>
        /// 获取指定的UI架构数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public UILayerData GetUILayerDataByKey(string key)
        {
            return UILayerList;
        }
    }
}
